/**
 * Created by xc on 2016/05/12.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbNotifyUser = function(obj){
    ItbBaseModel.call(this,obj);

    // 以下为表定义数据，需要的字段就填入
    //	 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '关联id',
    //   `notify_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '消息id',
    //   `user_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '用户id',
    //   `state` int(3) NOT NULL DEFAULT '0' COMMENT '状态：待定',
    //   `create_time` datetime NOT NULL,
    //   `update_time` datetime NOT NULL,
    //   `update_by` varchar(50) NOT NULL DEFAULT '0',
    //   `del_flg` int(1) NOT NULL DEFAULT '0',

};
ItbNotifyUser.prototype = new ItbBaseModel();

// 取得表名
ItbNotifyUser.prototype.getTableName = function(){
    return 'itb_notify_user';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbNotifyUser.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
}

//------------------------------------------------------------------
/* 获取通知消息
 * 参数 id
 * 参数 type
 * 参数 start_time: 预计开始日时, YYYYMMDDhhmmss (14位年月日十分秒)
 * 参数 end_time: 预计结束日时, YYYYMMDDhhmmss (14位年月日十分秒)
 * 参数 time: 预计日时, YYYYMMDDhhmmss (14位年月日十分秒)
 */
ItbNotifyUser.prototype.getMessage = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbNotifyUser.getMessage Err:'+err);
        }
        // Use the connection
        var sql = ' SELECT * FROM itb_notification t WHERE t.del_flg = 0 ';
        if (THIS.id) {
            sql += ' AND t.id= ' + THIS.id;
        }
        if (THIS.type) {
            sql += ' AND t.type= ' + THIS.type;
        }
        if (THIS.time || THIS.start_time || THIS.end_time) {
            if (THIS.start_time) {
                sql += ' AND t.start_time >= str_to_date("' + THIS.start_time + '","%Y%m%d%H%i%s") ';
            }
            if (THIS.end_time) {
                sql += ' AND t.end_time <= str_to_date("' + THIS.start_time + '","%Y%m%d%H%i%s") ';
            }
            if (THIS.time) {
                sql += ' AND t.start_time <= str_to_date("' + THIS.time + '","%Y%m%d%H%i%s") ';
                sql += ' AND t.end_time >= str_to_date("' + THIS.time + '","%Y%m%d%H%i%s") ';
            }
        } else {
            sql += ' AND t.start_time <= sysdate() ';
            sql += ' AND t.end_time >= sysdate() ';
        }

        if (THIS.order_by) {
            sql += ' ORDER BY ' + THIS.order_by;
        } else {
            sql += ' ORDER BY t.number ';
        }
        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

// 导出
module.exports = ItbNotifyUser;